<?php
/**
 * @Author: gan
 * @Description:
 * @File:  FeiEYunImpl
 * @Version: 1.0.0
 * @Date: 2022/5/24 9:25 上午
 */
declare(strict_types=1);

namespace RX\Component\CloudPrint\Impl;

use RX\Enums\CommonEnum;
use RX\Enums\NetworkEnum;
use RX\Exception\BizException;
use RX\Traits\CurlTrait;

class FeiEYunImpl
{
    use CurlTrait;

    protected string $host = 'http://api.feieyun.cn/Api/Open/';

    protected array $config;

    public function __construct(array $config)
    {
        if (!isset($config['user']) || !isset($config['ukey'])) {
            throw new BizException(CommonEnum::class, CommonEnum::INVALID_PARAMS, "请检查user与ukey是否配置");
        }
        $this->config = $config;
    }

    public function register(array $params)
    {
        return $this->request('Open_printerAddlist', $params);
    }

    public function delete(array $params)
    {
        return $this->request('Open_printerDelList', $params);
    }

    public function update(array $params)
    {
        return $this->request('Open_printerEdit', $params);
    }


    public function status(array $params)
    {
        return $this->request('Open_queryPrinterStatus', $params);
    }


    public function print(array $params, string $type = "")
    {
        if ($type == 'label') {
            return $this->labelMsg($params);
        }
        return $this->msg($params);
    }

    public function msg(array $params)
    {
        return $this->request('Open_printMsg', $params);
    }

    public function labelMsg(array $params)
    {
        return $this->request('Open_printLabelMsg', $params);
    }


    public function clean(array $params)
    {
        return $this->request('Open_delPrinterSqs', $params);
    }

    public function orderState(array $params)
    {
        return $this->request('Open_queryOrderState', $params);
    }

    public function orderInfoByDate(array $params)
    {
        return $this->request('Open_queryOrderInfoByDate', $params);
    }

    public function request(string $action, array $params = [])
    {
        $timestamp    = time();
        $publicParams = [
            'user'    => $this->config['user'],
            'stime'   => $timestamp,
            'sig'     => $this->getSign($timestamp),
            'apiname' => $action,
        ];
        $url          = $this->config['host'] ?? $this->host;
        $params       = array_filter(array_merge($publicParams, $params));
        $body         = self::postCurl($url, $params, false);
        return self::checkResponse($body);
    }

    protected function getSign($timestamp)
    {
        return sha1($this->config['user'] . $this->config['ukey'] . $timestamp);
    }

    protected function checkResponse($body): array
    {
        $body = json_decode($body, true);
        if (empty($body) || !is_array($body)) {
            throw new BizException(NetworkEnum::class, NetworkEnum::NETWORK_ERROR);
        }
        if ($body["ret"] !== 0) {
            throw new BizException(CommonEnum::class, CommonEnum::INVALID_PARAMS, $body["msg"] . "({$body["ret"]})");
        }
        return $body;
    }
}
